package com.pakaroid.android.app;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.util.Log;

public class PakaroidSettingAppWidgetUtil {
	private static final String TAG = "PakaroidSettingAppWidgetUtil";
	// wifi
	// 状態取得
	public static boolean isWifi(Context context){
		// 現在の状態取得する
		WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		return wifi.isWifiEnabled();
	}
	// 状態変更
	public static boolean toggleWifiSetting(Context context){
		// 現在の状態を反転してwifi状態をセットする
		WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		boolean wifiSetting = wifi.isWifiEnabled();
		wifi.setWifiEnabled(!wifiSetting);
    	return !wifiSetting;
	}
	
	// brightness
    //private static final int MINIMUM_BACKLIGHT = 20 + 10; // android.os.Power.BRIGHTNESS_DIM + 10;
    //private static final int MAXIMUM_BACKLIGHT = 255; // android.os.Power.BRIGHTNESS_ON
    //private static final int DEFAULT_BACKLIGHT = (int) (MAXIMUM_BACKLIGHT * 0.4f);
    
    
    private static final int SCREEN_BRIGHTNESS_MODE_MANUAL = 0;
    private static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;
    public static final String SCREEN_BRIGHTNESS = "screen_brightness";
    public static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";
    // 自動モードか取得
    public static boolean isAutoBrightnessMode(Context context) {
    	int brightnessMode = SCREEN_BRIGHTNESS_MODE_MANUAL;
    	ContentResolver cr = context.getContentResolver();
		try {
			if (context.getResources().getBoolean(com.android.internal.R.bool.config_automatic_brightness_available)) {
				brightnessMode = Settings.System.getInt(cr, SCREEN_BRIGHTNESS_MODE);
			}
		}catch(Exception e){
			
		}
    	return brightnessMode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
    }
    
    public static boolean toggleBrightness(Context context) {
    	int brightnessMode = SCREEN_BRIGHTNESS_MODE_MANUAL;
    	try {
    		ContentResolver cr = context.getContentResolver();

    		//Only get brightness setting if available
            if (context.getResources().getBoolean(com.android.internal.R.bool.config_automatic_brightness_available)) {
                brightnessMode = Settings.System.getInt(cr, SCREEN_BRIGHTNESS_MODE);
            }

            // Rotate AUTO -> MANUAL -> AUTO -> ...
            // Technically, not a toggle...
            if (brightnessMode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
            	brightnessMode = SCREEN_BRIGHTNESS_MODE_MANUAL;
            } else {
            	brightnessMode = SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
            }
            if (context.getResources().getBoolean(
            		com.android.internal.R.bool.config_automatic_brightness_available)) {
            	// Set screen brightness mode (automatic or manual)
            	Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE,
            			brightnessMode);
            } else {                    // Make sure we set the brightness if automatic mode isn't available
                brightnessMode = SCREEN_BRIGHTNESS_MODE_MANUAL;
            }
	    }catch (Settings.SettingNotFoundException e) {
            Log.d(TAG, "toggleBrightness: " + e);
        }
	    return brightnessMode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
    }
    
    // 縦横自動回転
    // 状態の取得
    public static boolean isAutoRotation(Context context) {
    	ContentResolver cr = context.getContentResolver();
    	return Settings.System.getInt(cr, Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
    }
    
    // 状態の変更
    public static boolean toggleAutoRotation(Context context) {
    	ContentResolver cr = context.getContentResolver();
    	Boolean isEnabled = isAutoRotation(context);
    	// 反転して設定
    	Settings.System.putInt(cr, Settings.System.ACCELEROMETER_ROTATION, isEnabled ? 0 : 1);
    	return !isEnabled;
    }
    
    
    
    // マナーモード
    // 状態の取得
    public static boolean isMannerMode(Context context){
    	AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    	int mode = manager.getRingerMode();
    	return mode == AudioManager.RINGER_MODE_VIBRATE;
    }
    
    // 状態の変更
    public static boolean toggleMannerMode(Context context){
    	// 現在の状況を保存
    	AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    	boolean mannerMode = isMannerMode(context);
    	if(!mannerMode){
    		// マナーモード設定
    		manager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    	}else{
    		// マナーモード解除
    		manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    	}
    	return !mannerMode;
    }
 
}
